<template>
    <div class="register_wrap">
        <div class="register">
            <el-form ref="form" :model="form" label-width="80px">
                <el-form-item label="用户名">
                    <el-input v-model.trim="form.name"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input placeholder="请输入密码" v-model.trim="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码">
                    <el-input placeholder="请输入密码" v-model.trim="form.repeat_password" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :plain="true" @click="onRegister">注册</el-button>
                    <el-button type="primary" @click="onReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { Message } from 'element-ui';
//引入封装好的基于axios发送请求的函数。
// import request from '@/api/request.js';
//引入封装好的专门注册的接口
import {req_register} from '@/api/registerLogin.js';
export default {
    data() {
        return {
            form: {
                name: '',
                password: '',
                repeat_password: ''
            }
        }
    },
    methods: {
        async onRegister() {
            //收集数据，判断密码与确认密码输入的是否一致，一致发送ajax请求，不一致则清空input中的值且弹窗警告。
            if (this.form.password == this.form.repeat_password) {
                let result = await req_register({
                    method: 'post',
                    url: '/register',
                    arg: {
                        user_name: this.form.name,
                        password: this.form.password
                    },
                    headerType: 'form',
                });
                console.log(result);
                this.$router.push({name:"login"})
            } else {
                Message({
                    message: '请重新输入密码',
                    type: 'warning'
                });
                this.form.password = "";
                this.form.repeat_password = "";
            }
        },
        onReset() {
            this.form.name = "";
            this.form.password = "";
            this.form.repeat_password = "";
        }
    }
}
</script>

<style lang="scss" scoped>
.register_wrap {
    display: flex;
    justify-content: center;
    align-items: center;
    background: url('~@/assets/img/register_bg.webp') no-repeat;
    background-size: 100% 100%;

    .register {
        background: url('~@/assets/img/bggrey.jpg') no-repeat;
        background-size: 100% 100%;
        width: 400px;
        height: 350px;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>